﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace SiparisKontrol
{
    public class Query : IDisposable
    {
        private DbSqlServer _dbsql;
        public Query()
        {
            _dbsql = new DbSqlServer();
        }
        public SqlTransaction TransAction { get; set; }
        List<SqlParameter> _prm2 = new List<SqlParameter>();
        private string _queryString = string.Empty;

        public Query Add(string sqlText)
        {
            this._queryString = sqlText;
            return this;
        }
        public void Clear()
        {
            this._queryString = string.Empty;
            this._prm2.Clear();
        }
        public Query Add(string name, SqlDbType type, object value)
        {
            _prm2.Add(new SqlParameter() { ParameterName = name, SqlDbType = type, Value = value });
            return this;
        }
        public Query Add(string name, SqlDbType type, object value, int size)
        {
            _prm2.Add(new SqlParameter() { ParameterName = name, SqlDbType = type, Value = value, Size = size });
            return this;
        }

        public List<T> ToList<T>(T sample)
        {
            if (this.TransAction != null)
                return _dbsql.ToList<T>(sample, this._queryString, TransAction, this._prm2.ToArray());
            else
                return _dbsql.ToList<T>(sample, this._queryString, this._prm2.ToArray());
        }
        public void Exec()
        {
            if (TransAction != null)
                _dbsql.Exec(_queryString, TransAction, _prm2.ToArray());
            else
                _dbsql.Exec(_queryString, null, _prm2.ToArray());
        }
        public T ExecScaler<T>()
        {
            if (TransAction != null)
                return _dbsql.ExecScalar<T>(_queryString, TransAction, _prm2.ToArray());
            else
                return _dbsql.ExecScalar<T>(_queryString, _prm2.ToArray());
        }
        public DataTable Fill()
        {
            if (TransAction != null)
                return _dbsql.Fill(_queryString, TransAction, _prm2.ToArray());
            else
                return _dbsql.Fill(_queryString, _prm2.ToArray());
        }
        public void Dispose()
        {
            GC.SuppressFinalize(_prm2);
            GC.SuppressFinalize(this);
        }
    }
}
